<?php

header("Content-type:application/json; charset=UTF-8");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

require('../service/content_service.php');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$wsname = $_REQUEST['wsname'];
$tokeyKey = $_REQUEST['token'];

class content_ws
{

    public function getAllContent()
    {
        $content_service = new content_service();
        $lists = $content_service->findAll();
        return apputil::getJsonDataResultUtil($lists, 'contents');
    }

    public function getContentById($id)
    {
        $content_service = new content_service();
        $lists = $content_service->findContentById($id);
        return apputil::getJsonDataResultUtil($lists, 'contents');
    }

    public function getContentByCategoryId($catid)
    {
        $content_service = new content_service();
        $lists = $content_service->findContentByCategoryId($catid);
        return apputil::getJsonDataResultUtil($lists, 'contents');
    }

    public function createContent($contentObject)
    {
        $content_service = new content_service();
        $flag = $content_service->create($contentObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function updateContent($contentObject)
    {
        $content_service = new content_service();
        $flag = $content_service->update($contentObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function deleteContent($contentObject)
    {
        $content_service = new content_service();
        $flag = $content_service->delete($contentObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function searchContentByCriteria($contentObject)
    {
        $content_service = new content_service();
        $lists = $content_service->findContentByCriteria($contentObject);
        return apputil::getJsonDataResultUtil($lists, 'contents');
    }

    public function isValidateToken($tokeyKey)
    {
        $content_service = new content_service();
        return $content_service->isValidateTokenKey($tokeyKey);
    }

}

//init class
$content_ws = new content_ws();

if ($content_ws->isValidateToken($tokeyKey)) {
    if ($wsname == 'getallcontent') {
        print_r($content_ws->getAllContent());
    } else if ($wsname == 'getcontentbyid') {
        print_r($content_ws->getContentById($_REQUEST['id']));
    } else if ($wsname == 'getcontentbycategoryid') {
        print_r($content_ws->getContentByCategoryId($_REQUEST['catid']));
    } else if ($wsname == 'createcontent' || $wsname == 'updatecontent') {
        $content = new content();
        $content->setTitle(isset($_REQUEST['title']) ? $_REQUEST['title'] : null);
        $content->setAlias(isset($_REQUEST['alias']) ? $_REQUEST['alias'] : null);
        $content->setTitleAlias(isset($_REQUEST['title_alias']) ? $_REQUEST['title_alias'] : null);
        $content->setIntrotext(isset($_REQUEST['introtext']) ? $_REQUEST['introtext'] : null);
        $content->setFulltext(isset($_POST['fulltext']) ? $_POST['fulltext'] : null);
        $content->setCatid(isset($_REQUEST['catid']) ? $_REQUEST['catid'] : null);
        $content->setUserid(isset($_REQUEST['userid']) ? $_REQUEST['userid'] : null);
        $content->setCreated(isset($_REQUEST['created']) ? $_REQUEST['created'] : null);
        $content->setModified(isset($_REQUEST['modified']) ? $_REQUEST['modified'] : null);
        $content->setPublishdate(isset($_REQUEST['publishdate']) ? $_REQUEST['publishdate'] : null);
        $content->setStatus(isset($_REQUEST['status']) ? $_REQUEST['status'] : null);

        if ($wsname == 'createcontent') {
            print_r($content_ws->createContent($content));
        } else if ($wsname == 'updatecontent') {
            $content->setId(isset($_REQUEST['id']) ? $_REQUEST['id'] : null);
            print_r($content_ws->updateContent($content));
        }

    } else if ($wsname == 'deletecontent') {
        $content = new content();
        $content->setId(isset($_REQUEST['id']) ? $_REQUEST['id'] : null);
        print_r($content_ws->deleteContent($content));

    } else if ($wsname == 'searchcontentbycriteria') {

        $content = new content();
        $content->setTitle(isset($_REQUEST['title']) ? $_REQUEST['title'] : '');
        $content->setAlias(isset($_REQUEST['alias']) ? $_REQUEST['alias'] :'');
        $content->setTitleAlias(isset($_REQUEST['title_alias']) ? $_REQUEST['title_alias'] :'');
        $content->setCatid(isset($_REQUEST['catid']) ? $_REQUEST['catid'] : null);

        print_r($content_ws->searchContentByCriteria($content));


    } else {
        echo 'web service error !!';
    }
} else {
    echo 'Your token is incorrect !!';
}
?>